﻿@page "/account/resetpassword"
@page "/account/resetpassword/{UserId}"
@layout LoginLayout

@inject NavigationManager navigationManager
@inject HttpClient Http
@inject IdentityAuthenticationStateProvider authStateProvider
@inject IMatToaster matToaster

<MatCard>
    <div class="logo">
        <img src="images/blazorboilerplate.svg" style="width:100px;" /><br />Blazor Boilerplate
        <br />
        <h4>Password Reset</h4>
    </div>
    <EditForm Model="@resetPasswordParameters" OnValidSubmit="@SendResetPassword">
        <DataAnnotationsValidator />
        <ValidationSummary />
        <fieldset>
            <div class="form-group">
                <MatTextField @bind-Value="@resetPasswordParameters.Password" Label="Password" Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group">
                <MatTextField @bind-Value="@resetPasswordParameters.PasswordConfirm" Label="Password Confirmation" Icon="lock_outline" IconTrailing="true" FullWidth="true" Required="true" Type="password"></MatTextField>
            </div>
            <div class="form-group d-flex justify-content-end">
                <MatButton Type="submit" Raised="true">Reset Password</MatButton>
            </div>
        </fieldset>
        <hr />
        <NavLink href="/">
            <h6 class="font-weight-normal text-center">Cancel</h6>
        </NavLink>
    </EditForm>
</MatCard>

@code {
    ResetPasswordDto resetPasswordParameters { get; set; } = new ResetPasswordDto();

    [Parameter]
    public string UserId { get; set; }

    protected override async Task OnInitializedAsync()
    {
        // Blazor does not have query string accessible parameters yet so a hack is needed. Token is too long for path parameter
        var absoluteUrl = navigationManager.BaseUri;
        var token = absoluteUrl.Substring(absoluteUrl.IndexOf("?token=") + 7);

        if (!string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(token))
        {
            resetPasswordParameters.Token = token;
            resetPasswordParameters.UserId = this.UserId;
        }
        else
        {
            matToaster.Add("Your url is missing the Reset Token. Fatal Error", MatToastType.Danger, "Reset Token is Missing");
        }
    }

    async Task SendResetPassword()
    {
        try
        {
            ApiResponseDto apiResponse = await authStateProvider.ResetPassword(resetPasswordParameters);

            if (apiResponse.StatusCode == 200)
            {
                matToaster.Add("Reset Password was Successful", MatToastType.Success);
                navigationManager.NavigateTo("/account/login");
            }
            else
            {
                matToaster.Add(apiResponse.Message, MatToastType.Danger, "Reset Password Failed");
            }
        }
        catch (Exception ex)
        {
            matToaster.Add(ex.Message, MatToastType.Danger, "Reset Password Failed");
        }
    }
}
